package com.duobaoyu.dby.supply.message.listener.hander.order;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.duobaoyu.dby.supply.biz.client.boundary.account.AccountBoundaryClient;
import com.duobaoyu.dby.supply.biz.client.boundary.account.bean.bo.OperatorInfoBO;
import com.duobaoyu.dby.supply.biz.client.boundary.account.bean.dto.OperatorInfoDTO;
import com.duobaoyu.dby.supply.biz.producer.constant.SupplyChainMQConstants;
import com.duobaoyu.dby.supply.biz.producer.message.OrderExtendSyncMessage;
import com.duobaoyu.dby.supply.core.rocketmq.handler.AbstractClusterMessageHandler;
import com.duobaoyu.dby.supply.db.entity.extend.OrderProductParamExtendInfo;
import com.duobaoyu.dby.supply.db.entity.order.OrderProduct;
import com.duobaoyu.dby.supply.db.repository.order.OrderProductRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 确认订单消息
 *
 * @author chenms
 * @date 2023/10/07 10:58
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class OrderExtendSyncMessageHandler extends AbstractClusterMessageHandler<OrderExtendSyncMessage> {

	private final AccountBoundaryClient accountBoundaryClient;
	private final OrderProductRepository orderProductRepository;

	@Override
	public String getTopic() {
		return SupplyChainMQConstants.SupplyChainTopic.ORDER_TOPIC;
	}

	@Override
	public String getTag() {
		return SupplyChainMQConstants.SupplyChainTopic.OrderTag.ORDER_EXTEND_SYNC;
	}

	@Override
	public boolean messageHandler(OrderExtendSyncMessage orderExtendSyncMessage, MessageExt messageExt) {
		List<OrderExtendSyncMessage.OrderExtendSync> orderExtendSyncList = orderExtendSyncMessage.getOrderExtendSyncList();
		for (OrderExtendSyncMessage.OrderExtendSync orderExtendSync :orderExtendSyncList) {
			String rechargeAccount = orderExtendSync.getRechargeAccount();
			OperatorInfoDTO operatorInfo = new OperatorInfoDTO();
			operatorInfo.setChannelCode(orderExtendSync.getChannelCode());
			operatorInfo.setPhoneNo(rechargeAccount);
			OperatorInfoBO operatorInfoBo = accountBoundaryClient.phoneOperatorInfo(operatorInfo);

			OrderProductParamExtendInfo.PhoneOperator phoneOperator = new OrderProductParamExtendInfo.PhoneOperator();
			OrderProductParamExtendInfo productParamExtendInfo = new OrderProductParamExtendInfo();
			phoneOperator.setOperatorType(operatorInfoBo.getOperatorType());
			phoneOperator.setOperatorName(operatorInfoBo.getOperatorName());
			phoneOperator.setCityName(operatorInfoBo.getChannelCityName());
			phoneOperator.setProvinceName(operatorInfoBo.getChannelProvinceName());
			phoneOperator.setFlowValue(null);
			productParamExtendInfo.setPhoneOperator(phoneOperator);

			List<OrderProduct> orderProducts = orderProductRepository.getBaseMapper().selectList(Wrappers.<OrderProduct>lambdaQuery().eq(OrderProduct::getOrderSn, orderExtendSync.getOrderSn()));
			if(CollectionUtils.isEmpty(orderProducts)){
				return Boolean.FALSE;
			}
			orderProducts.forEach(item -> item.setParamExtend(productParamExtendInfo));
			orderProductRepository.updateBatchById(orderProducts);
		}
		return Boolean.TRUE;
	}
}
